package org.e_gold.service.impl;

import org.apache.log4j.Logger;
import org.e_gold.dao.CountryDAO;
import org.e_gold.dao.LeagueDAO;
import org.e_gold.model.crawler.livescore.League;
import org.e_gold.service.LeagueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Thang Nguyen
 */
@Service
public class LeagueServiceImpl implements LeagueService {

	private static final Logger logger = Logger.getLogger(LeagueServiceImpl.class);

	@Autowired
	private LeagueDAO leagueDAO;

	@Autowired
	private CountryDAO countryDAO;

	@Override
	public League getLeaguePersisted(League l) {
		League ret = null;
		try{
			League league= leagueDAO.getUniqueByPropertyName("name", l.getName());
			if(league != null){
				league.setCountry(l.getCountry());
				leagueDAO.update(league);
			}else{
				league = new League(l.getName(), l.getCountry());
				league = leagueDAO.insert(league);
			}
			ret = league;
		}catch(Exception ex){
			logger.error(ex);
			ex.printStackTrace();
		}
		return ret;
	}

	@Override
	public void saveLeague(League league) {
		try{

		}catch(Exception ex){
			logger.error(ex);
			ex.printStackTrace();
		}
	}

}
